<?php
namespace app\admin\controller;

use app\common\model\AdminRoles;
use app\common\model\AdminResourcesController;
use app\common\model\AdminResourcesAction;
use app\common\component\admin\AdminModel;

class Authorization extends ControllerBase {
	public function _initialize() {
		$this -> checkLogin();

		$this -> checkAuthorization();
	}

	public function index() {
		$this -> view -> assign('adminRoles', AdminRoles::order('role_id ASC') -> paginate(10));

		return $this -> view -> fetch();
	}

	public function create() {
		if($this -> request -> isPost()) {
			$post = $this -> request -> post();
			$result = [];
			foreach($post['resources'] as $k => $v) {
				if(is_array($v)) {
					$result[$k] = '';
					foreach($v as $kk => $vv) {
						$result[$k] .= $kk . ',';
					}
					$result[$k] = rtrim($result[$k], ',');
				}
			}
			$post['resources'] = json_encode($result);
			
			if((new AdminRoles($post)) -> save()) {
				echo '<meta charset="utf-8"><script>alert("创建成功!");window.location.href="/shuashuale/authorization";</script>';
			} else {
				echo '<meta charset="utf-8"><script>alert("创建失败!");window.history.go(-1);</script>';
			}
		}

		$this -> view -> assign('resources', $this -> getResources());

		return $this -> view -> fetch();
	}

	public function update() {
		$adminRole = AdminRoles::get(['role_id' => $this -> request -> get('role_id')]);
		if(!$adminRole) {
			echo '<meta charset="utf-8"><script>alert("非法操作!");window.location.href="/shuashuale/authorization";</script>';
		}

		if($this -> request -> isPost()) {
			$post = $this -> request -> post();
			foreach($post['resources'] as $k => $v) {
				if(is_array($v)) {
					$result[$k] = '';
					foreach($v as $kk => $vv) {
						$result[$k] .= $kk . ',';
					}
					$result[$k] = rtrim($result[$k], ',');
				}
			}
			$post['resources'] = json_encode($result);

			$adminRole -> role_name = $post['role_name'];
			$adminRole -> description = $post['description'];
			$adminRole -> resources = $post['resources'];
			
			if($adminRole -> save()) {
				echo '<meta charset="utf-8"><script>alert("修改成功!");window.location.href="/shuashuale/authorization";</script>';
			} else {
				echo '<meta charset="utf-8"><script>alert("修改失败!");window.history.go(-1);</script>';
			}
		}

		$adminRole -> resources = json_decode($adminRole -> resources, true);
		$this -> view -> assign('adminRole', $adminRole);

		$this -> view -> assign('resources', $this -> getResources());

		return $this -> view -> fetch();
	}

	public function delete() {
		$adminRole = AdminRoles::get(['role_id' => $this -> request -> get('role_id')]);
		if(!$adminRole) {
			echo '<meta charset="utf-8"><script>alert("非法操作!");window.location.href="/shuashuale/authorization";</script>';
		}

		if($adminRole -> delete()) {
			$otherAdminRole = AdminRoles::find();

			$admins = AdminModel::where('role_id', $adminRole -> role_id) -> select();
			foreach($admins as $admin) {
				$admin -> role_id = $otherAdminRole -> role_id;
				$admin -> save();
			}

			echo '<meta charset="utf-8"><script>alert("删除成功!");window.location.href="/shuashuale/authorization";</script>';
		} else {
			echo '<meta charset="utf-8"><script>alert("删除失败!");window.history.go(-1);</script>';
		}
	}

	public function getResources() {
		$getAllAdminResourcesController =  AdminResourcesController::all();

		$resources = [];
		foreach($getAllAdminResourcesController as $adminResourcesController) {
			$resources[$adminResourcesController -> resource_controller_id] = $adminResourcesController;

			$getAction = AdminResourcesAction::where('resource_controller_id', $adminResourcesController -> resource_controller_id) -> select();
			if(empty($getAction)) {
				$resources[$adminResourcesController -> resource_controller_id]['actions'] = [];
			} else {
				$resources[$adminResourcesController -> resource_controller_id]['actions'] = $getAction;
			}
		}

		return $resources;
	}
}